package cn.zyx.controller;


import cn.zyx.entity.Movie;
import cn.zyx.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Movie)表控制层
 *
 * @author makejava
 * @since 2022-01-18 14:45:31
 */
@RestController
@RequestMapping("movie")
public class MovieController {
    /**
     * 服务对象
     */
    @Resource
    private MovieService movieService;

    @Autowired
    private ReturnPayApi returnPayApi;

    /**
     * 订单添加
     * @param movie
     * @return
     */
    @RequestMapping("add")
    public Movie add(@RequestBody Movie movie) {
        MovieController movieController = new MovieController();
//        String time = movieController.time();
        System.out.println(movie);
//        添加下单时间
//        movie.setCreatetime(time);
//        添加充值状态
        movieService.insert(movie);
        return movie;
    }



    /**
     * 根据wx手机号 查询
     */
    @RequestMapping("selphone")
    public List<Movie> selwxPhone(String wxPhone){
        return movieService.queryPhone(wxPhone);
    }
    /**
     * 根据订单编号查询
     */
    @RequestMapping("selopenid")
    public  List<Movie> selopenid(String openid){
        return movieService.queryOpenid(openid);
    }
    /**
     * 将未充值用户进行充值
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("cupd")
    public Map<String, Object> cupd(String id) {
        Movie movie = new Movie();
        movie.setId(id);
        movie.setMstatus(String.valueOf(1));
        MovieController movieController = new MovieController();
        String time = movieController.time();
        movie.setCreatetime(time);
        movieService.update(movie);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }


    /**
     * 将充值完成或者退款成功记录进行删除
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("dupd")
    public Map<String, Object> dupd(String id) {
        Movie movie = new Movie();
        movie.setId(id);
        movie.setIsdel(1);
        OilController oilController = new OilController();
        String time = oilController.time();
        movie.setCreatetime(time);
        movieService.update(movie);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("dupds")
    public Map<String, Object> dupds(String idsStr) {
        String[] split = idsStr.split(",");
        for (int i = 0; i < split.length; i++) {
            Movie movie = new Movie();
            String s = split[i];
            movie.setId(s);
            movie.setIsdel(1);
            movieService.update(movie);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 将充值失败用户进行退款
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("tupd")
    public Map<String, Object> tupd(String id, String orderNum, String money, String desc, HttpServletResponse response) {
        Movie movie1 = movieService.queryById(id);
//        int a = (int) (Float.valueOf(movie1.getTotaluserprice())*100);
//        returnPayApi.refund(movie1.getNumber(),"", String.valueOf(a),response);
//        Map<String, Object> refund = returnPayApi.refund(movie1.getNumber(), "", "1", response);
        System.out.println(111);
        System.out.println(id);
        Movie movie = new Movie();
        movie.setId(id);
        movie.setCode(String.valueOf(2));
        VipController VipController = new VipController();
        String time = VipController.time();
        movie.setCreatetime(time);
        movie.setNumber(orderNum);
        movie.setMmoney(money);
        movie.setDesc(desc);
        movieService.update(movie);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 地区名&充值金额下拉框&充值状态
     *
     * @param
     * @return
     */
    @RequestMapping("sel")
    public Map<String, Object> query(@RequestParam(defaultValue = "1") Integer page, Integer limit){

        List<Movie> movies = this.movieService.queryAllByLimit((page - 1) * limit, limit);
        Long count = movieService.count();
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg","查询成功");
        map.put("data", movies);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }

    public String time() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Movie selectOne(String id) {
        return this.movieService.queryById(id);
    }

}